<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ShopRepo;
use App\Http\Requests\ShopRequest;

class ShopController extends Controller
{
    public function index(Request $request)
    {
        $param = [];
    	$list = ShopRepo::getList();

    	return view('admin.shop.index',compact('list','param')); 
    }

    public function add()
    {
        return view('admin.shop.add');
    }

    /**
     * 保存新添加的商户
     */
    public function save(ShopRequest $request)
    {  
        $rs['status'] = false;
        $data = $request->pure(); 
        $data['status'] = 1;     
        if ($request->hasFile('logo')) {
            unset($data['logo']);
            $data['logo'] = ShopRepo::saveLogo($request->logo);        
        }      
       
        $flag = ShopRepo::create($data);
        if($flag['status']) {
            $rs['status'] = true;
            return redirect('zadmin/shop/list')->with('rs',$rs);
        } 
        return back()->with('rs',$rs);
    }

    public function edit($id)
    {
        $data = ShopRepo::find(['id'=>$id]);
        //dd($data);
        return view('admin.shop.edit',compact('data'));
    }

    /**
     * 冻结商户
     */
    public function frozen($id)
    {
        $rs['status'] = false;
        $flag = ShopRepo::frozen($id);
        if($flag) {
            $rs['status'] = true;
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    public function update(ShopRequest $request)
    {
        $rs['status'] = false;
        $rs['msg'] = '操作失败';
        $data = $request->pure();  
        $id = array_pull($data,'id');    
        if ($request->hasFile('logo')) {
            unset($data['logo']);
            $data['logo'] = ShopRepo::saveLogo($request->logo);        
        }      
        
        $where['id'] = $id;
        
        $flag = ShopRepo::update($where,$data);
        if($flag['status']) {
            $rs['status'] = true;
            $rs['msg'] = '操作成功';
            return redirect('zadmin/shop/list')->with('rs',$rs);
        } 

        return back()->with('rs',$rs);
    }

   
    

    /**
     * 商品订单
     */
    public function orders($shop_id)
    {
    	$list = ShopRepo::getOrders($shop_id);
    	return view('admin.shop.product',compact('list'));
    }

    /**
     * 商铺提现记录
     */
    public function withdraw($shop_id)
    {
    	$list = ShopRepo::getWithdraw($shop_id);
    	return view('admin.shop.product',compact('list'));
    }

    /**
     * 商铺红包记录
     */
    public function envelope($user_id)
    {
    	$list =  ShopRepo::getEnvelope($user_id);
    	return view('admin.shop.envelope',compact('list'));
    }

    public function check(Request $request)
    {
        $id = $request->id;

        ShopRepo::check($id);
        return back();
    }
}
